<template>
    <div class="login">
        <section class="form_container">
            <div class="manage_tip">
                <span class="title">志权后台管理系统</span>

                <el-form :model="loginUser" :rules="rules" ref="loginForm" label-width="80px" class="loginForm">
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="loginUser.email" placeholder="请输入邮箱"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="loginUser.password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登录</el-button>
                        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
                    </el-form-item>
                    <div class="tiparea">
                        <p>还没有账号?现在<router-link to="/register">注册</router-link></p>
                    </div>
                </el-form>
            </div>
        </section>
    </div>
</template>
<script>
import jwt_decode from 'jwt-decode'

export default {
  name: "Register",
  data () {
    return {
        loginUser:{
            password: '',
            email: '',
        },
        rules:{
            email: [
                {
                    require: true,
                    type: 'email',
                    message: '邮箱格式不正确',
                    trigger: 'blur'
                },

            ],
            password: [
                {
                    require: true,
                    message: '密码不能为空',
                    trigger: 'blur'
                },
                {
                    min: 6,
                    max: 15,
                    message: "长度在 6 到 15 个字符",
                    trigger: 'blur'
                }
            ],

        }
    };
  },
  methods: {
      submitForm(formName){
          this.$refs['loginForm'].validate((valid) => {
          if (valid) {
            this.$axios.post("/api/users/login",this.loginUser)
                       .then(res =>{
                          const { token } = res.data
                          if(res.data.status == '0'){
                              // 将 token 存储到 localstorage 中
                              localStorage.setItem("Token",token)   
                              
                             // 解析 token   
                              const decode = jwt_decode(token)
                              console.log(decode)
                             // 存储数据 
                              this.$store.dispatch("setIsAutnenticated",!this.isEmpty(decode))
                              this.$store.dispatch("setUser",decode)
                             // 页面跳转 
                              this.$router.push("/index")
                          }else{
                              alert('登录失败')
                              return false
                          }
                       })
          }
        });
      },
    //  判断 token 为空的函数
    isEmpty(value) {
      return (
        value === undefined ||
        value === null ||
        (typeof value === "object" && Object.keys(value).length === 0) ||
        (typeof value === "string" && value.trim().length === 0)
      );
    }
  },
}
</script>
<style scoped>
.login{
    position: relative;
    width: 100%;
    height: 100%;
    background: url("../assets/bg.jpg") no-repeat center center;
    background-size: 100% 100%;
}
.form_container{
    position: absolute;
    top: 20%;
    left: 38%;
    width: 360px;
    height: 210px;
    padding: 25px;
    text-align: center;

}
.form_container .manage_tip .title {
    color: #fff;
    font-family: "Microsoft YaHei";
    font-weight: bold;
    font-size: 25px;

}
.loginForm{
    margin-top: 20px;
    padding: 20px 40px 3px 10px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0px 5px 5px #cccc;
}
.submit_btn{
    width: 100%;
}
.tiparea{
    text-align: right;
    font-size: 12px;
    color: #333;
    margin-bottom: 10px;
}
.tiparea p a{
    color: #409eff;
}
</style>